'''
创建一个迭代器类和对象
'''
from collections.abc import Iterator

# 自定义一个迭代器类
class MyRange(object):
    '''编写一个自定义的range，模仿range的功能'''
    def __init__(self,stop_num):
        self.stop_num = stop_num # 结束数字，不包括
        self.num = -1 # 第一个数字从0开始

    def __iter__(self):
        print('__iter__')
        return self

    def __next__(self):
        '''返回下一条数据'''
        print('__next__')
        self.num += 1
        if self.num == self.stop_num:
            '''抛出一个异常'''
            raise StopIteration
        return self.num



if __name__ == '__main__':
    obj = MyRange(6)
    # print(isinstance(obj,Iterator))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))
    # print(next(obj))

    ''' for 循环的本质，就是循环一个可迭代的对象 '''
    for i in MyRange(6):
        print(i)
